<?php
/**
 * Created by PhpStrom
 * User: 魏惠东
 * Date: 2021/6/2 15:09
 * Desc:
 */

namespace App\Models\Admin;
use App\Models\BaseModel;
use App\Models\Admin\Post as PostModel;
use App\Models\Admin\Org as OrgModel;
use App\Models\User as UserModel;
class UserGroupMember extends BaseModel{
    protected $table = "user_group_member";
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    protected $fillable=["group_id","member_id","type","creator","name"];
    protected $declares = [
        "group_id" => ["group_id", "="],
    ];
    /**
     * 列表
     * @param $search 条件数组
     */
    public static function getList($limit,$search){
        $res = self::
        leftJoin('user_group as u','user_group_member.group_id','u.id')
            ->select('user_group_member.*','u.name as group_name')
            ->where(function($query) use ($search){
                if(!empty($search['group_id'])){
                    $query->where("group_id",$search['group_id']);
                }
            })
            ->orderBy('id','desc')->paginate($limit)->toArray();
        foreach ($res['data'] as $k=>$v){
            switch ($v['type']){
                case 'post':
                    $res['data'][$k]['name'] = PostModel::where("id",$v['member_id'])->value('name');
                    $res['data'][$k]['type_text'] = "岗位";
                    $res['data'][$k]['number'] = UserModel::where("post_id",$v['member_id'])->count();
                    break;
                case 'org':
                    $res['data'][$k]['name'] = OrgModel::where("id",$v['member_id'])->value('name');
                    $res['data'][$k]['type_text'] = "部门";
                    $res['data'][$k]['number'] = UserModel::where("org_id",$v['member_id'])->count();
                    break;
                case 'user':
                    $res['data'][$k]['name'] = UserModel::where("id",$v['member_id'])->value('username');
                    $res['data'][$k]['type_text'] = "人员";
                    $res['data'][$k]['number'] = 1;
                    break;
            }
        }
        return $res;
    }
    public static function calculationNumber($data){
        foreach ($data as $k=>$v){
            switch ($v['type']){
                case 'post':
                    $res[$k]['name'] = PostModel::where("id",$v['member_id'])->value('name');
                    $res[$k]['type_text'] = "岗位";
                    $res[$k]['number'] = UserModel::where("post_id",$v['member_id'])->count();
                    break;
                case 'org':
                    $res[$k]['name'] = OrgModel::where("id",$v['member_id'])->value('name');
                    $res[$k]['type_text'] = "部门";
                    $res[$k]['number'] = UserModel::where("org_id",$v['member_id'])->count();
                    break;
                case 'user':
                    $res[$k]['name'] = UserModel::where("id",$v['member_id'])->value('username');
                    $res[$k]['type_text'] = "人员";
                    $res[$k]['number'] = 1;
                    break;
            }
        }
        return $res;
    }
    public static function selectData($where){
        return self::where($where)->get();
    }
}
